package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.common.ServiceException;
import com.py.credit.dao.RemovalDao;
import com.py.credit.entity.Removal;
import com.py.credit.entity.Template;
import com.py.credit.service.RemovalService;
import com.py.credit.utils.SQLBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class RemovalServiceImpl implements RemovalService {

    @Autowired
    private RemovalDao removalDao;

    @Override
    public List<Map<String, Object>> selectByConditionForPage(String deptId, String jclb, String type, List<String> showColumns, String[] additionalColumns, String condition, Page page) {
        if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列");
        }
        List<String> conditions = SQLBuilder.createConditionSql(condition);

        return removalDao.selectByConditionForPage(deptId, jclb, type, showColumns, additionalColumns, conditions, page);
    }

    @Override
    public Removal selectInfo(String ajbh) {
        return removalDao.selectInfo(ajbh);
    }

    @Override
    public void update(String ajbh) {
        removalDao.update(ajbh, new Timestamp(System.currentTimeMillis()));
    }

    @Override
    public Integer selectMbId(String status) {
        return removalDao.selectMbId(status);
    }

    @Override
    public void add(String name, String type, int mbId, Long userId, Long deptId, String ajbh, Long operatorId, Long operatorDeptId) {
        removalDao.add(name, type, mbId, userId, deptId, ajbh, operatorId, operatorDeptId);
    }

    @Override
    public String getContent(Template template, String ajbh,String jclb,  String ztmc, String deptName, String status) {
        List<String> list = new ArrayList<String>();
        Map<String, Object> map = new HashMap<String, Object>();

        String content = "";
        String content1 = "";
        String aString2 = "";
        if (template != null) {
            content = template.getContent();
            content1 = template.getContent();
            String rgex = "(?<=\\{\\{)(.*?)(?=\\}\\})";
            Pattern p = Pattern.compile(rgex);
            Matcher m = p.matcher(content);
            while (m.find()) {
                list.add(m.group());
            }
        }

        boolean flag;

        if (jclb.equals("C")) {
            jclb="联合惩戒";
        }else{
            jclb="联合激励";
        }

        status="名单移出";

        map.put("事件编号", ajbh);
        map.put("主体名称", ztmc);
        map.put("发起部门", deptName);
        map.put("事件类别", jclb);
        map.put("事件状态", status);

        for (String in : map.keySet()) {
            //map.keySet()返回的是所有key的值
            Object str11 = new StringBuffer();
            str11 = map.get(in);//得到每个key多对用value的值
            String aString = "{{" + in + "}}";
            if (str11 == null) {
                aString2 = content1.replace(aString, "");
                content1 = aString2;
            } else {
                aString2 = content1.replace(aString, str11.toString());
                content1 = aString2;
            }

        }

        return content1;
    }

    @Override
    public List<Long> selectDpet(String ajbh) {
        return removalDao.selectDpet(ajbh);
    }

    @Override
    public List<Long> selectManagerDeptList(String id) {
        return removalDao.selectManagerDeptList(id);
    }
}
